import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Demo3 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        Thread t1 = new Thread(()->{
            //消费者
            try {
                while(true){
                    Integer value = queue.take();
                    System.out.println("消费: "+value);
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(()->{
            //生产者
            try {
                int count = 0;
                while (true){
                    queue.put(count);
                    System.out.println("生产: "+count);
                    count++;

                    Thread.sleep(1000);//每隔1s会生产一个元素
                    //生产者生产过快导致消费者跟不上时,生产者达到规定的数组大小,就会暂停生产,此时消费者消费一个,生产者才会生产一个
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}
